/**
 * 
 */
package tr.edu.ozyegin.ccrg.wekaPerformanceRun;

import java.io.IOException;
import java.util.Properties;

/**
 * The Weka process that will be executed via this command Implementation class
 * of Process Run Command Pattern
 * 
 * @author im1076
 * @version 0.2
 * @since 28.June.2011
 * @see ProcessRunCommand
 * 
 */
public class WekaProcessRunCommand implements ProcessRunCommand {

	private Properties wekaPerformanceProperties;

	//private static Object lock = new Object(); // this is to be used to get locks for local threads.

	/**
	 * Constructor
	 */
	public WekaProcessRunCommand() {
		// TODO Auto-generated constructor stub
		// at the class initialization the necessary weka parameters should be
		// read from the properties file.
		// then the weka execution parameters should be set at the class scope.

		wekaPerformanceProperties = TestController
				.getSingletonedTestContoller().getWekaPerformanceProperties();

	} // Constructor

	/**
	 * This method executes the Weka process that will run during the test
	 * Through this method the Weka process execution parameters can be set via
	 * function call.
	 * 
	 * @param _wekaJarPath
	 *            The path of the Weka Jar
	 * @param _wekaTestAlgorithm
	 *            The Weka ML Algorithm that will run
	 * @param _wekaTestDataFile
	 *            The weka data file that will run through ML Algorithm
	 * 
	 * @see #execute() which reads the parameters from properties file
	 * 
	 * @since 15.07.2011
	 */

	public Process execute(String _wekaJarPath, String _wekaTestAlgorithm,
			String _wekaTestDataFile) {
		// TODO Auto-generated method stub

		long startTime = 0;
		long endTime = 0;
		long duration = 0;

		Process p = null;
		try {
 
				startTime = System.nanoTime();

				p = Runtime.getRuntime().exec(
						"java -cp " + _wekaJarPath + " " + _wekaTestAlgorithm
								+ " -t " + _wekaTestDataFile + " -i");

				endTime = System.nanoTime();
				duration = endTime - startTime;

				TestController.getSingletonedTestContoller()
						.incrementTotalSerialRuntime(duration);
				TestController.getSingletonedTestContoller()
						.incrementThreadRuntime(duration);

 

		} catch (IOException e) {
			// TODO Auto-generated catch block
			
			// Cannot run program "java": java.io.IOException: error=24, Too many open files
			// http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/
			
			e.printStackTrace();
			
			TestController.getSingletonedTestContoller().incrementIOExceptionAmount();			
		}

		return p;
	} // end of method

	/**
	 * Process run command that runs the weka process without the path
	 * parameters. The path parameters are extracted from properties file.
	 * 
	 * @see #execute(String, String, String)
	 * 
	 * @since 15.07.2011
	 */
	@Override
	public Process execute() {

		String wekaJarPath = wekaPerformanceProperties
				.getProperty("wekaJarPath");
		String wekaTestAlgorithm = wekaPerformanceProperties
				.getProperty("wekaTestAlgorithm");
		String wekaTestDataFile = wekaPerformanceProperties
				.getProperty("wekaTestDataFile");

		return execute(wekaJarPath, wekaTestAlgorithm, wekaTestDataFile);

	} // end of method

 

} // end of class
